package utilidades;

import java.text.ParseException;
import java.util.Date;
import java.util.GregorianCalendar;

public class FormatarData {

	public static String formatar(GregorianCalendar data) throws ParseException {

		Date d = data.getTime();
		
		java.text.SimpleDateFormat dtfGeral = new java.text.SimpleDateFormat("dd/MM/yyyy");

		String geral = dtfGeral.format(d);

		return geral;

	}
	
	public static GregorianCalendar inserirData(String data) {
		
		String[] res = data.split("/");
		
		GregorianCalendar datag = new GregorianCalendar(Integer.parseInt(res[2]),
				Integer.parseInt(res[1]) - 1,
				Integer.parseInt(res[0]));
		
		return datag;
		
	}
	
	public static String getMesAno(String data) {
		
		String[] mesano = data.split("/");
		String res = mesano[1] + "/" + mesano[2];
		return res;
		
	}
	
	public static String getMesAno(Date data) {
		
		String[] mesano = data.toString().split("-");
		String res = mesano[1] + "/" + mesano[0];
		return res;
		
	}
	
	public static GregorianCalendar formataDataLayout(String data) {
		
		String[] res = data.split("/");
		
		res[1] = "20" + res[1];
		
		GregorianCalendar data_formatada = new GregorianCalendar();
		data_formatada.set(Integer.parseInt(res[1]), Integer.parseInt(res[0]) - 1, GregorianCalendar.DATE);
		int dia = data_formatada.getActualMaximum(GregorianCalendar.DATE);
		data_formatada.set(GregorianCalendar.DAY_OF_MONTH, dia);
		
		return data_formatada;
		
	}
	
	public static GregorianCalendar formataDataLayout_2(String data) {
		
		String[] res = data.split("/");
		
		res[2] = "20" + res[2];
		
		GregorianCalendar data_formatada = new GregorianCalendar();
		data_formatada.set(Integer.parseInt(res[2]), Integer.parseInt(res[1]) - 1, GregorianCalendar.DATE);
		int dia = data_formatada.getActualMaximum(GregorianCalendar.DATE);
		data_formatada.set(GregorianCalendar.DAY_OF_MONTH, dia);
		
		return data_formatada;
		
	}

	public static GregorianCalendar formataDate(java.sql.Date date) {
		
		System.out.println(date.toString());
		
		String[] res = date.toString().split("-");
		
		GregorianCalendar data_formatada = new GregorianCalendar();
		data_formatada.set(Integer.parseInt(res[0]), Integer.parseInt(res[1]) - 1, Integer.parseInt(res[0]));
		
		return data_formatada;
		
	}

	public static int getMesAnoINT(String mesini) {
		
		String[] mesano = mesini.split("/");
		String res = mesano[1] + mesano[2];
		
		int result = Integer.parseInt(res);
		
		return result;
		
	}

	public static int toInt(String periodo) {
		
		String[] mesano = periodo.split("/");
		String res = mesano[0] + mesano[1];
		
		int result = Integer.parseInt(res);
		
		return result;
		
	}
}
